<?php


namespace api\app\controller;

use api\app\middleware;

use api\app\model\AddressModel;
use api\app\validate\Vildate;
use cmf\controller\BaseController;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;


use think\facade\Filesystem;
use think\Request;

class AddressController extends BaseController
{
    public function add(Request $request)
    {
        $userid=$request->hello;

        $data = input('post.');
        $validate = new Vildate();
        $result = $validate->scene('login')->check($data);
        if (!$result) {
            return error($validate->getError());
        }
        $data['user_id']=$userid;
        if (AddressModel::create($data)) {
            $user = AddressModel::where('mobile',$data['mobile'])->find();
            $token=signToken($user['id'],$data['mobile']);
            return success('添加成功',$token);
        } else {
            return error('添加失败');
        }
    }
    public function editaddress(Request $request){
        $userid=$request->hello;

        $data = input('post.');
        $validate = new Vildate();
        $result = $validate->scene('login')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        $list = AddressModel::find($userid);
        if(!$list){
            $this->error('未查询的记录');
        }
        if($list->save()){
            return success('保存成功',$list);
        }else{
            return error('保存失败');
        }
    }
    public function getaddressInfo(Request $request)
    {
        $userid=$request->hello;
        $data = input('post.');
        $id = $data['id'];
        if (empty($id)) {
            return error('参数错误');
        }
        $news = AddressModel::where('user_id',$userid)
            ->select();
        if (!$news) {
            return error('账号不存在');
        }
        return success('返回成功',$news);
    }
    public function del(Request $request)
    {
        $userid=$request->hello;
        $id = input('id/d');
        $news =AddressModel::destroy($userid);
        if ($news) {
            return success('删除成功');
        } else {
            return success('删除失败');
        }
    }
}